<template>
  <div class="edit-popup">
    <popup
      ref="popupRef"
      :title="popupTitle"
      :async="true"
      width="550px"
      @confirm="handleSubmit"
      @close="handleClose"
    >
      <el-form ref="formRef" :model="formData" label-width="90px">
        <el-form-item label="礼物" prop="image">
          <el-image
            style="width: 100px; height: 100px"
            :src="formData.image"
            fit="fill"
            v-if="formData.image"
            class="mr_10"
          />

          <el-button type="primary" @click="openGiftTable">
            <template #icon>
              <el-icon>
                <Pointer />
              </el-icon>
            </template>
            选择礼物
          </el-button>
          <p style="width: 100%" class="mt_10">{{ formData.gift_name }}</p>
        </el-form-item>
        <!-- <el-form-item label="类型" prop="type">
          <el-select v-model="formData.type" clearable placeholder="请选择类型">
            <el-option
              v-for="item in options"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </el-form-item>-->
        <el-form-item label="票数/人气值" prop="number">
          <el-input v-model="formData.number" type="number" clearable placeholder="请输入礼物名称" />
        </el-form-item>
        <el-form-item label="价格" prop="price">
          <el-input v-model="formData.price" type="number" clearable placeholder="请输入礼物名称" />
        </el-form-item>
        <el-form-item label="排序" prop="sort">
          <el-input v-model="formData.sort" type="number" clearable placeholder="请输入礼物名称" />
        </el-form-item>
      </el-form>
    </popup>
  </div>
  <el-dialog v-model="giftTableVisible" title="选择助力礼物" width="800">
    <el-table ref="singleTableRef" :data="pager.lists" stripe border max-height="68vh">
      <!-- <el-table-column property="id" label="id" align="center" /> -->
      <el-table-column property="name" label="名称" align="center" />
      <el-table-column property="name" label="图片" align="center" width="100">
        <template #default="{ row }">
          <el-image :src="row.image" fit="fill" />
        </template>
      </el-table-column>
      <el-table-column property="address" label="操作" align="center">
        <template #default="{ row }">
          <el-button type="primary" @click="handleCurrentChange(row)">选择</el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination v-model="pager" class="mt_10" @change="getLists" />
  </el-dialog>
</template>

<script lang="ts" setup name="zgGiftEdit">
import { usePaging } from "@/hooks/usePaging";
import type { FormInstance } from "element-plus";
import Popup from "@/components/popup/index.vue";
import {
  accessListsAdd,
  accessListsDetail,
  accessListsUpdate,
} from "@/api/zg_assist_list";
import { apiZgGiftLists } from "@/api/zg_gift";
import { timeFormat } from "@/utils/util";
import type { UploadProps } from "element-plus";
import type { PropType } from "vue";
const route = useRoute();

defineProps({
  dictData: {
    type: Object as PropType<Record<string, any[]>>,
    default: () => ({}),
  },
});
const options = [
  {
    value: 1,
    label: "选手票数",
  },
  {
    value: 2,
    label: "用户票数",
  },
];
const emit = defineEmits(["success", "close"]);
const formRef = shallowRef<FormInstance>();
const popupRef = shallowRef<InstanceType<typeof Popup>>();
const mode = ref("add");

// 弹窗标题
const popupTitle = computed(() => {
  return mode.value == "edit" ? "编辑助力" : "新增助力";
});

//  打开礼物列表弹框
const giftTableVisible = ref(false);
const giftTable = ref<any>([]);
const queryParams = reactive({
  title: "",
  type: "",
  status: "0",
});
const openGiftTable = async () => {
  giftTableVisible.value = true;
  await getLists();
};

// 分页相关
const { pager, getLists, resetParams, resetPage } = usePaging({
  fetchFun: apiZgGiftLists,
  params: queryParams,
});

// 表单数据
const formData = reactive({
  id: "",
  activity_id: route.query.id,
  gift_id: "",
  image: "",
  gift_name: "",
  type: "",
  number: "",
  price: "",
  sort: "",
});

// 选中的礼物
const handleCurrentChange = (val: any) => {
  formData.gift_id = val.id;
  formData.image = val.image;
  formData.gift_name = val.gift_name;
  giftTableVisible.value = false;
};
// 表单验证
const formRules = reactive<any>({
  name: [
    {
      required: true,
      message: "请输入礼物名称",
      trigger: ["blur"],
    },
  ],
  image: [
    {
      required: true,
      message: "请输入礼物图片",
      trigger: ["blur"],
    },
  ],
});

// 获取详情
const setFormData = async (data: Record<any, any>) => {
  for (const key in formData) {
    if (data[key] != null && data[key] != undefined) {
      //@ts-ignore
      formData[key] = data[key];
    }
  }
};

const getDetail = async (row: Record<string, any>) => {
  const data = await accessListsDetail({
    id: row.id,
  });
  setFormData(data);
};

// 提交按钮
const handleSubmit = async () => {
  await formRef.value?.validate();
  const data = { ...formData };
  mode.value == "edit"
    ? await accessListsUpdate(data)
    : await accessListsAdd(data);
  popupRef.value?.close();
  emit("success");
};

//打开弹窗
const open = (type = "add") => {
  mode.value = type;
  popupRef.value?.open();
};

// 关闭回调
const handleClose = () => {
  emit("close");
};

defineExpose({
  open,
  setFormData,
  getDetail,
});
</script>
<style scoped>
.avatar-uploader .avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>

<style>
.avatar-uploader .el-upload {
  border: 1px dashed var(--el-border-color);
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
  transition: var(--el-transition-duration-fast);
}

.avatar-uploader .el-upload:hover {
  border-color: var(--el-color-primary);
}

.el-icon.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  text-align: center;
}
</style>
